package org.example.hashAndSet;

/**
 * 把链表分为 小于 等于  大于  三个区
 */
public class Code02_SmallerEqualBigger {
    public static class Node {
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }
    }
    public static Node ListPartition1(Node head,int pivot) {
        if(head == null) {
            return null;
        }
        Node cur = head;
        int i = 0;
        //计算链表的长度
        while (cur != null) {
            i++;
            cur = cur.next;
        }
        Node[] nodeArr = new Node[i];
        i = 0;
        cur = head;
        for(i = 0;i!= nodeArr.length;i++) {
            nodeArr[i] = cur;
            cur = cur.next;
        }
        arrPartition(nodeArr,pivot);
        for(i = 1; i!= nodeArr.length;i++) {
            nodeArr[i - 1] = nodeArr[i];

        }
        nodeArr[i - 1].next = null;
        return nodeArr[0];
    }

    private static void arrPartition(Node[] nodeArr, int pivot) {
        int small = -1;
        int big = nodeArr.length;
        int index = 0;
        while (index != big) {
            if(nodeArr[index].value < pivot) {
                swap(nodeArr,++small,index++);
            }else if(nodeArr[index].value ==pivot) {
                index++;
            }else {
                swap(nodeArr,--big,index);
            }
        }
    }

    private static void swap(Node[] nodeArr, int i, int index) {
        Node tmp = nodeArr[i];
        nodeArr[i] = nodeArr[index];
        nodeArr[index] = tmp;
    }


    public static Node ListPartition2(Node head,int pivot) {
        Node sH = null;
        Node sT = null;
        Node eH = null;
        Node eT = null;
        Node mH = null;
        Node mT = null;
        Node next = null;

        while (head != null) {
            next = head.next;
            head.next = null;
            if(head.value < pivot) {
                if(sH == null) {
                    sH = head;
                    sT = head;
                } else {
                    sT.next = head;
                    sT = head;
                }
            } else if(head.value == pivot) {
                if(eH == null) {
                    eH = head;
                    eT = head;
                }else {
                    eT.next = head;
                    eT = head;
                }
            }else {
                if(mH == null) {

                    mT = head;
                    mH = head;
                } else {
                    mT.next = head;
                    mT = head;
                }
            }
            head = next;
        }
        if (sT != null) {
            sT.next = eH;
            eT =  eT ==null?sT:eT;

        }
        if(eT != null) {
            eT.next = mH;
        }

        return sH!= null ? sH :(eH != null ? eH : mH);
    }
}
